package com.fsxgt.datagrid.component.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.executor.ExecutorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson2.JSONObject;
import com.fsxgt.datagrid.component.bo.DataWorkBO;
import com.fsxgt.datagrid.component.service.impl.DataWorkService;
import com.fsxgt.datagrid.core.exception.DataGridException;
import com.fsxgt.datagrid.core.page.PageParam;
import com.fsxgt.datagrid.core.utils.ReturnT;
import com.fsxgt.datagrid.ds.bo.DatasourceJdbcBO;
import com.fsxgt.datagrid.ds.service.impl.DatasourceJdbcService;
import com.fsxgt.datagrid.ds.service.impl.MetaService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Api(tags = "数据集成接口", value = "DataWorkController")
@RequestMapping(produces = "application/json; charset=UTF-8",path = "/dataWork")
@RestController
public class DataWorkController extends BaseController {

	@Autowired
	private DatasourceJdbcService datasourceJdbcService;
	@Autowired
	private DataWorkService dataWorkService;
	
	
	@Autowired
	private MetaService metaService;
 

	@ApiOperation(hidden = true, value = "查找数据源", notes = "")
	@RequestMapping(path = "/findDatasource", method = RequestMethod.POST)
	public ReturnT findDatasource(@RequestBody JSONObject param) throws DataGridException, IOException, ExecutorException {
		
		
		List list = datasourceJdbcService.findDatasource(new DatasourceJdbcBO());

		
		return new ReturnT(list);

	}
	
	@ApiOperation(hidden = true, value = "查找表", notes = "")
	@RequestMapping(path = "/findTableNames", method = RequestMethod.POST)
	public ReturnT findTableNames(@RequestBody JSONObject param) throws Exception {
		Long datasourceId=param.getLong("datasourceId");
		String schema=param.getString("schema");
		
		Map map=new HashMap();
		
		DatasourceJdbcBO bo = datasourceJdbcService.getDatasourceById(param.getLong("datasourceId"));

		List<String> schemas=metaService.findSchemas(bo);
		
		if(StringUtils.isEmpty(schema)&&schemas!=null&&schemas.size()>0) {
			;
		}else {
			List tables = metaService.findTableNames(bo,schema);
			map.put("tables", tables);
		}
		
		map.put("schemas", schemas);
		
		return new ReturnT(map);

	}
	
	@ApiOperation(hidden = true, value = "查找字段", notes = "")
	@RequestMapping(path = "/findColumns", method = RequestMethod.POST)
	public ReturnT findColumns(@RequestBody JSONObject param) throws Exception  {
		List list = null;
		
		DatasourceJdbcBO bo = datasourceJdbcService.getDatasourceById(param.getLong("datasourceId"));
		
		String schema = param.getString("schema");
		
		if(!StringUtils.isEmpty(param.getString("tableName"))) {
			list = metaService.findColumnNames(bo,schema,param.getString("tableName"));
		}else {
			list = metaService.findColumnNamesForSql(bo,param.getString("srcSqlText"));
		}
		
		return new ReturnT(list);

	}
	
	@ApiOperation(hidden = true, value = "保存", notes = "")
	@RequestMapping(path = "/saveDataWork", method = RequestMethod.POST)
	public ReturnT saveDataWork(@RequestBody DataWorkBO bo) throws DataGridException, IOException, ExecutorException, NumberFormatException, ExecutionException {
		
		

		dataWorkService.saveDataWork(bo);
		
		return new ReturnT();

	}
	
	@ApiOperation(hidden = true, value = "删除", notes = "")
	@RequestMapping(path = "/deleteDataWork", method = RequestMethod.POST)
	public ReturnT deleteDataWork(@RequestBody DataWorkBO bo) throws DataGridException, IOException, ExecutorException, NumberFormatException, ExecutionException {
		
		dataWorkService.deleteDataWork(bo.getId());
		
		return new ReturnT();

	}
	
	
	@ApiOperation(hidden = true, value = "获取数据", notes = "")
	@RequestMapping(path = "/getDataWork", method = RequestMethod.POST)
	public ReturnT getDataWork(@RequestBody DataWorkBO dataWorkBO) throws DataGridException, IOException, ExecutorException, NumberFormatException, ExecutionException {
		
		DataWorkBO bo = dataWorkService.getDataWork(dataWorkBO.getId());
		
		return new ReturnT(bo);

	}
	
	@ApiOperation(hidden = true, value = "分页查询", notes = "")
	@RequestMapping(path = "/findDataWorkPage", method = RequestMethod.POST)
    public ReturnT findDataWorkPage(@RequestBody PageParam<DataWorkBO> pageParam) {
		
		
		PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
		
		List<DataWorkBO> list=dataWorkService.findJobDataWorkPage(pageParam.getParam());
		
		PageInfo<DataWorkBO> pageInfo = new PageInfo<>(list);
		
		
        return new ReturnT(pageInfo);
    }
	

}